﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

namespace Tesla.Util.UIUtilities
{

    [ToolboxData("<{0}:TeslaDropDown runat=server></{0}:TeslaDropDown>")]
    public class TeslaDropDown:DropDownList
    {
        #region Variables
        private string default_Selection="Please select your ";
        //bool isDirty = false;
        int i = 0;
        #endregion


        #region CUSTOM PROPERTIES

        [Category("Behavior")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Category
        {
            get
            {
                String s = (String)ViewState["Category"];
                return ((s == null) ? "[" + "Empty" + "]" : s);
            }

            set
            {
                ViewState["Category"] = value;
            }
        }


        /// <summary>
        ///PUBLIC PROPERTY TO SET OR GET THE DIRTY PROPERTY OF THE CONTROL
        /// </summary>

        //[Category("Behavior")]
        //[DefaultValue(true)]
        //[Localizable(true)]
        //public bool Dirty
        //{
        //    get
        //    {
        //        return ViewState["isDirty"] == null ? false : (bool)ViewState["IsDirty"];
        //        //return (bool)ViewState["IsDirty"];

        //    }
        //    set
        //    {
        //        ViewState["isDirty"] = value;
        //    }
        //}

         #endregion


        #region overridden methods

        
        protected override void OnLoad(EventArgs e)
        {
                default_Selection = default_Selection + Category;
                
                if(!this.Items.Contains(new ListItem(default_Selection)))
                    this.Items.Insert(0, new ListItem(default_Selection));
                
          
            base.OnLoad(e);

        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute("_Dirty", "false");
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!Page.ClientScript.IsClientScriptBlockRegistered("ValidateDropdown"))
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Setting_Dirty", "function Setting_Dirty(ct1)" + "{document.getElementById(ct1).setAttribute(\"_Dirty\",\"true\");}", true);
            //base.OnPreRender(e);
        }


        protected override void Render(HtmlTextWriter writer)
        {
                this.Attributes.Add("onChange", "Setting_Dirty('" + this.ID + "')");
                base.Render(writer);
        }

        #endregion
    }
}

